library(tidyverse)
library(tidycensus)
library(leaflet)
library(sf)
library(stringr)Insert your own API Key. You may request a free key from the Census API tool https://api.census.gov/data/key_signup.html
#census_api_key("did you enter your API key here?")I recommend using one of the following variables. However, you can use the tidycensus::load_variables() and follow the “Searching for variables” instructions.
B08103_001E - MEDIAN AGE BY MEANS OF TRANSPORTATION TO WORK: Total: Taxicab, motorcycle, bicycle, or other means: Workers 16 years and over – (Estimate)B08131_001E - AGGREGATE TRAVEL TIME TO WORK (IN MINUTES) OF WORKERS BY PLACE OF WORK–STATE AND COUNTY LEVEL: Worked in State of residence: Workers 16 years and over who did not work at home – (Estimate)B19013_001E - median household incomeLoad the variable and assign and object name for some USA county using the get_acs function.
census_variable <-
get_acs(geography = "county",
variables = "B08103_001E",
state = "NC",
geometry = TRUE)
census_variableUsing the estimate variable in the census_variable sf object, generate a MapPalette.
MapPalette <- colorBin(palette = "viridis",
domain = census_variable$estimate,
bins = 4,
pretty = TRUE,
reverse = TRUE)Make choropleth by filling county polygons (census geography) with correlated value (from the ACS)
census_variable %>%
st_transform(crs = "+init=epsg:4326") %>%
leaflet(width = "100%") %>%
addProviderTiles(provider = "CartoDB.Positron") %>%
addPolygons(popup = ~ str_extract(NAME, "^([^,]*)"),
stroke = FALSE,
smoothFactor = 0,
fillOpacity = 0.7,
color = ~ MapPalette(estimate)) %>%
addLegend("bottomright",
pal = MapPalette,
values = ~ estimate,
title = "Median Age of<br>Travelers by<br>Taxi/Moto/Bike",
opacity = 1)AGGREGATE TRAVEL TIME TO WORK (IN MINUTES)
census_variable <-
get_acs(geography = "county",
variables = "B08131_001E",
state = "NC",
geometry = TRUE)## Getting data from the 2012-2016 5-year ACS
## Downloading feature geometry from the Census website. To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
census_variableAGGREGATE TRAVEL TIME TO WORK (IN MINUTES)
Convert Minutes to hours, hours to months.
census_variable <- census_variable %>%
rename(estimate_original = estimate) %>%
mutate(estimate = estimate_original / 60 / 730.0008)MapPalette <- colorBin(palette = "viridis",
domain = census_variable$estimate,
bins = 7,
pretty = TRUE,
reverse = TRUE)census_variable %>%
st_transform(crs = "+init=epsg:4326") %>%
leaflet(width = "100%") %>%
addProviderTiles(provider = "CartoDB.Positron") %>%
addPolygons(popup = ~ str_extract(NAME, "^([^,]*)"),
stroke = FALSE,
smoothFactor = 0,
fillOpacity = 0.7,
color = ~ MapPalette(estimate)) %>%
addLegend("bottomright",
pal = MapPalette,
values = ~ estimate,
title = "Agregate Travel Time<br>in Months",
opacity = 1)census_variable <-
get_acs(geography = "county",
variables = "B19013_001E",
state = "NC",
geometry = TRUE)## Getting data from the 2012-2016 5-year ACS
## Downloading feature geometry from the Census website. To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
census_variableMapPalette <- colorNumeric(palette = "viridis",
domain = census_variable$estimate,
reverse = TRUE)census_variable %>%
st_transform(crs = "+init=epsg:4326") %>%
leaflet(width = "100%") %>%
addProviderTiles(provider = "CartoDB.Positron") %>%
addPolygons(popup = ~ str_extract(NAME, "^([^,]*)"),
stroke = FALSE,
smoothFactor = 0,
fillOpacity = 0.7,
color = ~ MapPalette(estimate)) %>%
addLegend("bottomright",
pal = MapPalette,
values = ~ estimate,
title = "Median Household Income",
labFormat = labelFormat(prefix = "$"),
opacity = 1)